package com.kpi.yellow.reporting;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.WorkbookUtil;

/**
 * Hello world!
 *
 */
public class XLSDemo 
{
    public static void main( String[] args )
    {
        long startReport = System.currentTimeMillis();
        HSSFWorkbook reportBook = new HSSFWorkbook();
        String dataSheetName = WorkbookUtil.createSafeSheetName("Demo");
        Sheet dataSheet = reportBook.createSheet(dataSheetName);
        Row headerDateTime = dataSheet.createRow(1);
        
        Font defaultFont = reportBook.createFont();
        defaultFont.setFontName("Calibri");
        defaultFont.setFontHeightInPoints((short) 11);
        
        CellStyle defaultCellStyle = reportBook.createCellStyle();
        defaultCellStyle.setFont(defaultFont);
        
        Cell repGeneratedCell = headerDateTime.createCell(0);
        repGeneratedCell.setCellStyle(defaultCellStyle);
        Cell dateCell = headerDateTime.createCell(1);
        String formattedDateTime = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(new Date());
        Cell repGeneratedInCell = headerDateTime.createCell(2);
        Cell generationTimeCell = headerDateTime.createCell(3);
     
        dataSheet.addMergedRegion(new CellRangeAddress(1,1,0,3));
        
        repGeneratedCell.setCellValue("Report generated: " + formattedDateTime + " in " 
                + (System.currentTimeMillis() - startReport) + "ms");
      
        dataSheet.autoSizeColumn(0, true);
        
        FileOutputStream fileOut = null;
        try {
            fileOut = new FileOutputStream("examples\\Demo-Report.xls");
            reportBook.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        finally {
            try { 
                fileOut.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println("Report succesfully generated");
    } 

}
    